﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;

namespace UniRlv.UI.Main.BasicInfo
{
    public partial class FrmAddOrEditPackage : UniRlv.UI.Main.DialogBase
    {
        PackageRule info = null;
        public ProductInfo CurProduct { get;set;}

        public FrmAddOrEditPackage()
        {
            InitializeComponent();
        }

        private void FrmAddOrEditPackage_Load(object sender, EventArgs e)
        {
            lstGG.Items.AddRange(AppHelper.GetGG(CurProduct.gg));
            UIHelper.BindingComboBox2Setting(lstMinPackUnit, "unit", "，");
            UIHelper.BindingComboBox2Setting(lstMinTagUnit, "unit", "，");

            lblInfo.Text = "通用名：" + CurProduct.tym + "\r\n批准文号：" + CurProduct.pzwh;

            if (this.Tag == null)
            {
                lstGG.SelectedIndex = 0;
                lstMinPackUnit.SelectedIndex = 0;
                lstMinTagUnit.SelectedIndex = 0;
            }
            else
            {
                info = this.Tag as PackageRule;
                ShowInfo(info);

                //不允许修改规格
                lstGG.Enabled = false;
            }
        }

        private void ShowInfo(PackageRule info)
        {
            lstGG.SelectedItem = info.gg;
            lstMinPackUnit.SelectedItem = info.minPackUnit;
            lstMinTagUnit.SelectedItem = info.minTagUnit;
            txtMinTagGG.Text = info.minTagGG;

            txtRemarks.Text = info.remarks;
            numTagLevel.Value = info.tagLevel;

            int[] ints = AppHelper.GetRatios(info.tagRatio);

            if (ints.Length>1)
            {
                numRatio2.Value = ints[1];
            }

            if (ints.Length > 2)
            {
                numRatio3.Value = ints[2];
            }

            if (ints.Length > 3)
            {
                numRatio4.Value = ints[3];
            }
        }

        private void numTagLevel_ValueChanged(object sender, EventArgs e)
        {
            int tagLevel = Convert.ToInt32(numTagLevel.Value);
            switch (tagLevel)
            {
                case 1:
                    lblcolon2.Visible = false;
                    numRatio2.Visible = false;

                    lblcolon3.Visible = false;
                    numRatio3.Visible = false;

                    lblcolon4.Visible = false;
                    numRatio4.Visible = false;
                    break;
                case 2:
                    lblcolon2.Visible = true;
                    numRatio2.Visible = true;

                    lblcolon3.Visible = false;
                    numRatio3.Visible = false;

                    lblcolon4.Visible = false;
                    numRatio4.Visible = false;
                    break;
                case 3:
                    lblcolon2.Visible = true;
                    numRatio2.Visible = true;

                    lblcolon3.Visible = true;
                    numRatio3.Visible = true;

                    lblcolon4.Visible = false;
                    numRatio4.Visible = false;
                    break;
                case 4:
                    lblcolon2.Visible = true;
                    numRatio2.Visible = true;

                    lblcolon3.Visible = true;
                    numRatio3.Visible = true;

                    lblcolon4.Visible = true;
                    numRatio4.Visible = true;
                    break;
                default:
                    break;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (info == null)
            {
                info = new PackageRule();
                info.ID = DALOpr.GetNextObjectID("PackageRule");
                info.productID = CurProduct.ID;
                info.createTime = DateTime.Now;
                info.creator = PublicObject.CurUser.name;
                info.state = 1;
            }

            info.gg = lstGG.Text;
            info.remarks = txtRemarks.Text;
            info.minPackUnit = lstMinPackUnit.Text;
            info.minTagUnit = lstMinTagUnit.Text;
            info.minTagGG = txtMinTagGG.Text;
            info.tagLevel = Convert.ToInt32(numTagLevel.Value);
            info.tagPackRatio = Convert.ToInt32(numTagPackRatio.Value);

            int r2, r3, r4;
            if (info.tagLevel == 4)
            {
                r4 = Convert.ToInt32(numRatio4.Value);
                r3 = Convert.ToInt32(numRatio3.Value);
                r2 = Convert.ToInt32(numRatio2.Value);

                if (r4%r3!=0)
	            {
		            ShowWarnMessageBox("提示", "包装比例不合法");
                    numRatio4.Focus();
                    return;
	            }

                if (r3%r2!=0)
	            {
		            ShowWarnMessageBox("提示", "包装比例不合法");
                    numRatio3.Focus();
                    return;
	            }

                info.tagRatio = "1:"+r2.ToString()+":"+r3.ToString()+":"+r4.ToString();
            }

            if (info.tagLevel == 3)
            {
                r3 = Convert.ToInt32(numRatio3.Value);
                r2 = Convert.ToInt32(numRatio2.Value);

                if (r3 % r2 != 0)
                {
                    ShowWarnMessageBox("提示", "包装比例不合法");
                    numRatio3.Focus();
                    return;
                }

                info.tagRatio = "1:" + r2.ToString() + ":" + r3.ToString();
            }

            if (info.tagLevel == 2)
            {
                info.tagRatio = "1:" + numRatio2.Value.ToString();
            }

            if (info.tagLevel == 1)
            {
                info.tagRatio = "1";
            }

            this.Tag = info;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}
